using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Krystalware.SlickUpload.Status;
using Krystalware.SlickUpload;
using Adge.Common;

namespace Adge.Web.UI.Admin
{
    public partial class onlineManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ListUsrPaperStat();
            }
        }

        private void ListUsrPaperStat()
        {

            DirectoryInfo dirInfo = new DirectoryInfo(HttpContext.Current.Server.MapPath(AdgeConfig.UserPaperStatDir));

            repFiles.DataSource = dirInfo.GetFiles();
            repFiles.DataBind();

        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(HttpContext.Current.Server.MapPath(AdgeConfig.UserPaperStatDir));

            UploadStatus status = HttpUploadModule.GetUploadStatus();
            string fileFullName = status.GetUploadedFiles()[0].LocationInfo["fileName"];
            string fileName = Path.GetFileName(fileFullName);
            try
            {
                File.Move(fileFullName, Path.Combine(dirInfo.FullName, fileName));
            }
            catch (Exception ex)
            {
                lblStatus.Text = ex.Message;
                //throw;
            }
            
        }

        protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            //reset index
            repFiles.CurrentPageIndex = e.NewPageIndex;
            ListUsrPaperStat();
        }

        protected void repFiles_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            string fileName = e.CommandArgument.ToString();
            //Response.Write(filename);
            
            try
            {                
                File.Delete(fileName);                
            }
            catch (Exception ex)
            {
                lblStatus.Text = ex.Message;
            }

            ListUsrPaperStat();
        }
    }
}
